package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.BtyTxl;
import cn.stylefeng.guns.sys.modular.system.service.BtyAqjjService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 安全基金控制器
 *
 * @author 张凯
 * @date 2020/8/5
 */
@Controller
@RequestMapping("/btyAqjj")
public class BtyAqjjController extends BaseController {

    private static final String PREFIX = "/modular/system/btyAqjj/";

    @Autowired
    private BtyAqjjService btyAqjjService;

    @RequestMapping("")
    public String index() {
        return PREFIX + "btyAqjj.html";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String ctbh, String dm) {
        //获取用户的帐号
        String account = LoginContextHolder.getContext().getUser().getAccount();
        Page<Map<String, Object>> list = this.btyAqjjService.list(ctbh, dm);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }


    @RequestMapping(value = "/btyAqjj_update/{id}")
    public String btyTxl_update(@PathVariable Long id, Model model) {
        BtyTxl btyTxl = this.btyAqjjService.getByID(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyTxl));
        LogObjectHolder.me().set(btyTxl);
        return PREFIX + "btyAqjj_edit.html";
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    public Object update(BtyTxl btyTxl) {
        if (ToolUtil.isOneEmpty(btyTxl, btyTxl.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.btyAqjjService.update(btyTxl);
        return SUCCESS_TIP;
    }
}
